package cn.gmcx.lazy.izze.service.impl;

import cn.gmcx.lazy.izze.enums.ConcernEnum;
import cn.gmcx.lazy.izze.enums.DynamicTypeEnum;
import cn.gmcx.lazy.izze.enums.RespCodeEnum;
import cn.gmcx.lazy.izze.exception.BizException;
import cn.gmcx.lazy.izze.mapper.CommentsMapper;
import cn.gmcx.lazy.izze.mapper.UserMapper;
import cn.gmcx.lazy.izze.pojo.*;
import cn.gmcx.lazy.izze.mapper.VideoMapper;
import cn.gmcx.lazy.izze.service.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * <p>
 * 视频 服务实现类
 * </p>
 *
 * @author GMCX
 * @since 2024-06-14
 */
@Service
public class VideoServiceImpl extends ServiceImpl<VideoMapper, Video> implements IVideoService {

    @Autowired
    private VideoMapper videoMapper;

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private FileService fileService;

    @Autowired
    private IDynamicService dynamicService;

    @Autowired
    private IFansService fansService;

    @Autowired
    private IPraiseService praiseService;

    @Autowired
    private CommentsMapper commentsMapper;


    @Override
    public Integer add(MultipartFile file, Integer userId, Integer type, String synopsis) {
        String videoUrl = fileService.getVideo_url2(file);
        String newSynopsis;
        int addIsOk = videoMapper.add(videoUrl, userId, type, synopsis);
        if (addIsOk != 0) {
            if (synopsis.length() > 10) {
                newSynopsis = synopsis.substring(9) + "...";
            } else {
                newSynopsis = synopsis;
            }
            dynamicService.addDynamic(userId, DynamicTypeEnum.CREATE, "视频名为" + newSynopsis);
        }
        return addIsOk;
    }

    @Override
    public List<VideoVo> selectRecommend(Integer id) {

        List<VideoVo> videoVoList = videoMapper.selectRandom();
        videoVoList.forEach(item -> {
            Integer userId = item.getUserId();
            Integer videoId = item.getId();
            Boolean isPraise = praiseService.getIsPraise(id, videoId);

            ConcernEnum concern = fansService.isConcern(userId, id);

            User user = userMapper.selectById(userId);
            user.setPassword("******");
            item.setIsPraise(isPraise);
            item.setIsConcern(concern);
            item.setUser(user);
        });

        return videoVoList;
    }

    @Override
    public void updatePraise(Integer userId, Integer videoId, Boolean isPraise) {


        if (!isPraise) {
            QueryWrapper<Praise> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("user_id", userId);
            queryWrapper.eq("video_id", videoId);
            long count = praiseService.count(queryWrapper);
            if (count == 0) {
                Integer isSuccess = praiseService.addPraise(userId, videoId);
                if (isSuccess != 0) {
                    videoMapper.updatePraise(videoId);
                }
            }
        } else {
            QueryWrapper<Praise> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("user_id", userId);
            queryWrapper.eq("video_id", videoId);
            Praise one = praiseService.getOne(queryWrapper);
            if (one != null) {
                boolean b = praiseService.removeById(one.getId());
                if (b) {
                    videoMapper.cancelPraise(videoId);
                }
            }
        }


    }

    @Override
    public void updateComments(Integer videoId) {
        QueryWrapper<Comments> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("video_id",videoId);
        Long count = commentsMapper.selectCount(queryWrapper);
        videoMapper.updateComments(videoId,count);
    }

}
